import sys
import os
sys.path.append(os.getcwd())

import numpy as np

class LonLatEulerMeasure:
    def __init__(self, threshold) -> None:
        self.threshold = threshold
        pass

    def __call__(self, lon1, lat1, lon2, lat2) -> bool:
        lon_diff = abs(lon1-lon2)
        lon_diff = lon_diff if lon_diff<360.0-lon_diff else 360.0-lon_diff
        lat_diff = abs(lat1-lat2)

        distance = (lon_diff**2 + lat_diff**2)**0.5
        if distance > self.threshold :
            return False
        else:
            return True

# 经度差值<lon_threshold 且 纬度差值<lat_threshold 返回True
class LonLatDiffMeasure:
    def __init__(self, lon_threshold, lat_threshold) -> None:
        self.lat_threshold = lat_threshold
        self.lon_threshold = lon_threshold
        pass

    def __call__(self, lon1, lat1, lon2, lat2) -> bool:
        lon_diff = abs(lon1-lon2)
        lon_diff = lon_diff if lon_diff<360.0-lon_diff else 360.0-lon_diff
        lat_diff = abs(lat1-lat2)

        if lon_diff <= self.lon_threshold and lat_diff <= self.lat_threshold:
            return True
        else:
            return False

class TopicSimilarity:
    def __init__(self) -> None:
        pass

    def get_topic_set(self, topics1, topics2):
        topic_set = set()
        for t in topics1:
            topic_set.add(t)
        for t in topics2:
            topic_set.add(t)
        return topic_set
    def get_feature_dim(self, topic_set):
        return len(topic_set)
    def get_topic_feature(self, topic_set: set, topics: list):
        dim = self.get_feature_dim(topic_set)
        feature = np.zeros(dim, dtype=float)
        for index, t in enumerate(topic_set):
            if t in topics:
                feature[index] = 1
        return feature
            

    def __call__(self, topics1, topics2):
        if len(topics1) == 0 or len(topics2) == 0:
            return 0
        topic_set = self.get_topic_set(topics1, topics2)
        feature1 = self.get_topic_feature(topic_set, topics1)
        feature2 = self.get_topic_feature(topic_set, topics2)

        f1 = np.mat(feature1)
        f2 = np.mat(feature2)
        num = float(f1 * f2.T)
        if (num==0):
            return 0
        
        denom = np.linalg.norm(f1)* np.linalg.norm(f2)
        cos_theta = num/denom
        sim = 0.5+0.5*cos_theta
        return sim

        
        